package com.star.pay.controller;

import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import com.star.entity.Result;
import com.star.entity.StatusCode;
import com.star.pay.config.RabbitMQConfig;
import com.star.pay.service.WXPayService;
import com.star.util.ConvertUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

*/
@RestController
@RequestMapping("/wxpay")
public class WXPayController {

    @Autowired
    private WXPayService wxPayService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 下单
     * @param orderId
     * @param money
     * @return
     */
    @GetMapping("/nativePay")
    public Result nativePay(@RequestParam("orderId") String orderId,@RequestParam("money") Integer money) {
        Map resultMap = wxPayService.nativePay(orderId, money);
        return new Result(true, StatusCode.OK, "下单成功",resultMap);
    }

    /**
     * 处理微信回调数据
     * @param request
     * @param response
     */
    @RequestMapping("/natify")
    public void natifyLogic(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("支付回调成功");
        try {
            //输入流转为字符串
            String xml = ConvertUtils.convertToString(request.getInputStream());
            //基于微信回调消息处理改变订单状态
            Map<String, String> map = WXPayUtil.xmlToMap(xml); //微信自带xml解析工具

            if (map.get("result_code").equals("SUCCESS")) {
                Map result = wxPayService.queryOrder(map.get("out_trade_no"));
                System.out.println("查询订单的结果：" + result);

                if (result.get("result_code").equals("SUCCESS")) {
                    //携带信息
                    Map message = new HashMap();
                    message.put("orderId", result.get("out_trade_no"));
                    message.put("transactionId", result.get("transaction_id"));
                    //将订单消息发送到MQ
                    rabbitTemplate.convertAndSend("", RabbitMQConfig.ORDER_PAY, JSON.toJSONString(message));
                    //完成双向通信
                    rabbitTemplate.convertAndSend("paynotify", "", result.get("out_trade_no"));
                } else {
                    //输出错误原因
                    System.out.println(result.get("err_code_des"));
                }

            } else {
                //输出错误原因
                System.out.println(map.get("err_code_des"));
            }
            //返回通知告知微信已处理
            response.setContentType("text/xml");
            String data = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
            response.getWriter().write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
